package com.sumainfor.pc.epc.service.impl.FeignMkt;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.JsonFromObject;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.MktFeign.HousingPlanMapper;
import com.sumainfor.pc.epc.dao.UtilsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 * 房源装修配置
 * @Auther: zhlu
 * @Date: 2018/12/19 16:41
 * @Description:
 */
@Service
public class HousingPlanServiceImpl {
    private ReentrantLock lock = new ReentrantLock(false);
    String UUID=null;
    Logger log= LoggerFactory.getLogger(HousingPlanServiceImpl.class);

    @Autowired
    HousingPlanMapper housingPlanMapper;

    @Autowired
    UtilsMapper utilsMapper;

    /**
     * 功能描述:
     * 新增房源配置
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/19 16:43
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddHousingPlan(Map<String,Object> params){
        boolean result=false;
        lock.lock();
        try {
            try {
                List<Map<String, Object>> PalHouList = (List<Map<String, Object>>) params.get("PalHouList");
                for(Map<String, Object> map:PalHouList){
                    //子编号
                    int i=0;
                    Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                        put("uuid", "PalHouId");
                        put("database", Constants.DATA_SOURCE_TCEPC);
                        put("surface", "tcepc_housing_plan");
                    }});
                    if(uuid!=null){
                        int number=Integer.valueOf(uuid.get("uuid").toString());
                        UUID= ComUtils.getLocalTrmSeqNum("fypz",number);
                    }else{
                        UUID=ComUtils.getLocalTrmSeqNum("fypz",0);
                    }
                    //所有新增处理
                    Map<String,Object>commonInsertParms=new HashMap<String,Object>();
                    commonInsertParms.put("by", ContextUtils.getUserId());//操作人
                    commonInsertParms.put("PalHouId",UUID);//房源配置主表
                    commonInsertParms.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    commonInsertParms.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(commonInsertParms);
                    map.putAll(commonInsertParms);
                    map.put("PalHouId",UUID);
                    if(map.get("HouConType").equals(1)){//公共区域
                        map.put("HouConName","公共区域");
                    }else{
                        map.put("HouConName","房间"+map.get("cod"));
                    }
    //                //新增房源配置主表
                    housingPlanMapper.AddHousingPlan(map);
    //                //房源配置详表
                    List<Map<String, Object>> PalHouInfoList = (List<Map<String, Object>>) map.get("PalHouInfoList");
                    for(Map<String, Object> maps:PalHouInfoList){
                        i++;
                        maps.putAll(commonInsertParms);
                        maps.put("PalHouId",UUID);
                        maps.put("HouConIds",i);
                        //新增房源配置主表
                        housingPlanMapper.AddHousingPlanInfo(maps);
                    }
                }
                result=true;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 功能描述: 
     * 根据房源编号获取房源套餐
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/22 12:27
     */
    public List<Map<String,Object>>getHousingPlanList(Map<String,Object>params){
        List<Map<String,Object>>getHousingPlanList=housingPlanMapper.getHousingPlanList(params);
        for(Map<String,Object> map:getHousingPlanList){
            List<Map<String,Object>>getHousingPlanInfoList=housingPlanMapper.getHousingPlanInfoList(new HashMap<String,Object>(){{
                put("PalHouId",map.get("PalHouId"));
            }});
            for (Map<String,Object> maps:getHousingPlanInfoList){
                if(maps.get("SupType")!=null){
                    maps.put("WiFi", JsonFromObject.FromObjMap(maps.get("WiFi").toString()));
                }
            }
            map.put("HousingPlanInfoList",getHousingPlanInfoList);
        }
        return getHousingPlanList;
    }

    /**
     * 功能描述:
     * 获取配置变更的房源存入Redis
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/24 15:21
     */
    public List<Map<String,Object>> getHousingPlanAmendList(Map<String,Object>params){
        List<Map<String,Object>>getHousingPlanAmendList=housingPlanMapper.getHousingPlanAmendList(params);
        return getHousingPlanAmendList;
    }

    /**
     * 功能描述:
     * 收房专员已读修改房源配置表状态为已读
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/26 11:22
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateHousingPlanRead(Map<String,Object>params){
        boolean result=false;
        //所有修改处理
        Map<String,Object>commonUpdateParms=new HashMap<String,Object>();
        commonUpdateParms.put("by", ContextUtils.getUserId());//操作人
        commonUpdateParms.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
        commonUpdateParms.put("fnc",
                this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
        ParamsCommon.commonUpdateParms(commonUpdateParms);

        //房源配置
        try {
            List<Map<String, Object>> HousingPlanList = (List<Map<String, Object>>) params.get("HousingPlanList");
            for(Map<String, Object> map:HousingPlanList){
                //修改配置已读
                map.put("IfRead",1);
                housingPlanMapper.updateHousingPlanRead(map);
            }
            result=true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

}
